<?php
/**
 * @author shutyuka@gmail.com
 * @modified 2011-02-20
 */
class Staff{
	
	static public $FLAG_INACTIVE = 0;
	
	static public $FLAG_UNACTIVE = 10;
	
	static public $TABLE_NAME="staffs";

	static public function Create($staff) {
		$staff['create_time'] = time();
		if( $staff['email'] != null ){
			$user_row = array();
			$user_row['email'] = $staff['email'];
			$user_row['password'] = '123456';
			$user_row['family_name'] = $staff['family_name'];
			$user_row['personal_name'] = $staff['personal_name'];
			$user_row['personal_name'] = $staff['personal_name'];
			$user_row['status'] = Users::$STATUS_STAFF;
			Logs::setLog(" set  status  ==".Users::$STATUS_STAFF );
			$tempId = Users::Create($user_row);
			
			OauthLimit::Create($tempId, 10);
		}
		
		$staff['id'] = DB::Insert(self::$TABLE_NAME, $staff);
		
		return $staff['id'];
	}
	
	static public function getById($staffId){
		$staff = DB::GetTableRow(self::$TABLE_NAME, array(
					'id' => $staffId,
		));
		return $staff;
	}
	
	static public function Update($staff) {
		$preStaff = self::getById($staff['id']);
		if( $preStaff['email'] != $staff['email'] && $staff['email'] != null){
			$preUser = Users::GetUserByEmail($preStaff['email']);
			if( $preUser != null ){
				$preUser['email'] = $staff['email'];
				$preUser['password'] = '123456';
				Users::Modify($preUser['id'], $preUser);
			}else{
			
				$user_row = array();
				$user_row['email'] = $staff['email'];
				$user_row['password'] = '123456';
				$user_row['family_name'] = $staff['family_name'];
				$user_row['personal_name'] = $staff['personal_name'];
				$user_row['status'] = Users::$STATUS_STAFF;
				$tempId = Users::Create($user_row);
				
				OauthLimit::Create($tempId, 10);
			}
		}
		
		Table::UpdateCache(self::$TABLE_NAME, $staff['id'], $staff);
		
		return ;
	}
	
	static public function GetAll(){
		$oc = array('activity' => 0);
		$all = DB::LimitQuery(self::$TABLE_NAME, array(
		'condition' => $oc,
		'order' => 'ORDER BY id ASC'
				));
		return $all;
	}
	
	static public function GetAllByShop($shopId){
		$oc = array('activity' => 0, 'shop_id' => $shopId);
		$all = DB::LimitQuery(self::$TABLE_NAME, array(
			'condition' => $oc,
			'order' => 'ORDER BY id ASC'
				));
		return $all;
	}
	
	static public function GetAllByShopAndService($shopId, $serviceId){
			
		
		$sql = "select staffs.id as id, staffs.family_name as family_name, staffs.personal_name as personal_name from staffs, staff_skill where staffs.shop_id=$shopId and staff_skill.service_id=$serviceId and staff_skill.staff_id = staffs.id;" ;
		//Logs::setLog(" staff log is || $sql  || ");
		return DB::GetQueryResult($sql, false);
		
	}
	
	static public function GetCountByTimeAndService($shopId, $serviceId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute){
		$startTime = mktime($startHour, $startMinute, 0, $month, $day, $year);
		$endTime = mktime($endHour, $endMinute, 0, $month, $day, $year);
		if($serviceId > 0){
			$sql = "select count(1) as count from ".
			"staffs, staff_skill,work_schedule where ".
			"staffs.shop_id=$shopId and staff_skill.service_id=$serviceId and staff_skill.staff_id = staffs.id and ".
			"staffs.id=work_schedule.staff_id and work_schedule.start_time <= $startTime and work_schedule.end_time >= $endTime" ;
			
			//Logs::setLog(" $year || $month || $day || $startMinute || $startHour || $endMinute || $endHour");
			//Logs::setLog(" staff new log is|| $sql ||");
			return DB::GetQueryResult($sql, false);
		}else{
			$sql = "select count(1) as count from ".
			"staffs, work_schedule where ".
			"staffs.shop_id=$shopId and staffs.id=work_schedule.staff_id and work_schedule.start_time <= $startTime and work_schedule.end_time >= $endTime" ;
			
			//Logs::setLog(" $year || $month || $day || $startMinute || $startHour || $endMinute || $endHour");
			//Logs::setLog(" staff new log is|| $sql ||");
			return DB::GetQueryResult($sql, false);
		}
		
	}
	
	static public function GetAbsenceCountByTimeAndService($shopId, $serviceId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute){
		$startTime = mktime($startHour, $startMinute, 0, $month, $day, $year);
		$endTime = mktime($endHour, $endMinute, 0, $month, $day, $year);
		if($serviceId > 0){
			$sql = "select count(1) as count from ".
			"staffs, staff_skill,absence where ".
			"staffs.shop_id=$shopId and staff_skill.service_id=$serviceId and staff_skill.staff_id = staffs.id and ".
			"staffs.id=absence.staff_id and !(absence.start_time >= $endTime or absence.end_time <= $startTime)" ;
			
//			Logs::setLog(" $year || $month || $day || $startMinute || $startHour || $endMinute || $endHour");
//			Logs::setLog(" staff new log is|| $sql ||");
			return DB::GetQueryResult($sql, false);
		}else{
			$sql = "select count(1) as count from ".
			"staffs, absence where ".
			"staffs.shop_id=$shopId and staffs.id=absence.staff_id and !(absence.start_time >= $endTime or absence.end_time <= $startTime)" ;
			
//			Logs::setLog(" $year || $month || $day || $startMinute || $startHour || $endMinute || $endHour");
//			Logs::setLog(" staff new log is|| $sql ||");
			return DB::GetQueryResult($sql, false);
		}
		
	}
	

	static public function SetStaffUnactive($staffId) {
		if (!$staffId) 
			return array();
		$staff = self::getById($staffId);	
		if( empty($staff['email']) ){
			$tempUser = Users::GetUserByEmail($staff['email']);
			$tempUser['status'] = Users::$STATUS_DEL;
			
			Users::Modify($tempUser['id'], $tempUser);
		}
		Table::UpdateCache(self::$TABLE_NAME, $staffId, array(
			'activity' => self::$FLAG_UNACTIVE)
		);
		StaffSkill::Delete($staffId);
		//Logs::setLog("    has done delete $staffId");
		return ;
	}
	
	
	static public function SetStaffInActive($staffId) {
		if (!$staffId) 
			return array();
		Table::UpdateCache(self::$TABLE_NAME, $staffId, array(
			'activity' => self::$FLAG_INACTIVE)
		);
		return ;
	}
	
}
